package xmwxapplet

import (
	"github.com/gin-gonic/gin"
	"github.com/gogap/logs"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/goods"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/room"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/utils"
	"yundian/internel/pkg/wxpay"
)

// HandleGetStewardUseChargeCouponList 查询会员充电可用优惠券
func HandleGetStewardUseChargeCouponList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetWxUseChargeCouponListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.Openid, _ = helper.GetUserNameFromCtx(ctx)
	param.MemberId = userId

	if data := dao.NewCouponDao().WxAutoReceiveCoupon(&request.GetWxReceiveCouponListReq{
		PlaceId: param.PlaceId, DeviceSn: param.DeviceSn, MemberId: param.MemberId,
	}); data.Err != nil {
		Response(ctx)("", CodeError, false)(OK)
		return
	}

	data := dao.NewCouponDao().GetWxChargeCouponList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// 查询可用优惠券
func HandleGetStewardUseCouponList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetWxReceiveCouponListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId = userId

	data := dao.NewCouponDao().GetWxReceiveCouponList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

func HandleStewardReceiveCoupon(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.WxReceiveCouponReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId = userId

	data := dao.NewCouponDao().WxReceiveCoupon(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

func HandleGetStewardCouponList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetWxReceiveCouponListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId = userId

	data := dao.NewCouponDao().GetWxReceiveCouponList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetStewardAllBulletinList 查询公告列表
func HandleGetStewardAllBulletinList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.BulletinListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	data := dao.NewBulletinDao().GetWxBulletinList(param, false)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// HandleGetStewardHomeBulletinList 查询公告列表
func HandleGetStewardHomeBulletinList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.BulletinListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	data := dao.NewBulletinDao().GetWxBulletinList(param, true)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

func HandleEmptyGoodsBuyCart(ctx *gin.Context) {
	param := &request.SetGoodsBuyCartReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId, _ = helper.GetUserIdFromCtx(ctx)

	if data := dao.NewGoodsDao().EmptyGoodsBuyCart(param); data.Err != nil {
		Response(ctx)("", CodeError, true)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

func HandleDeleteGoodsBuyCart(ctx *gin.Context) {
	param := &request.SetGoodsBuyCartReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId, _ = helper.GetUserIdFromCtx(ctx)

	if data := dao.NewGoodsDao().DeleteGoodsBuyCart(param); data.Err != nil {
		Response(ctx)("", CodeError, true)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleSetGoodsBuyCart 购物车-新增/更新
func HandleSetGoodsBuyCart(ctx *gin.Context) {
	param := &request.SetGoodsBuyCartReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId, _ = helper.GetUserIdFromCtx(ctx)

	if data := dao.NewGoodsDao().SetGoodsBuyCart(param); data.Err != nil {
		Response(ctx)("", CodeError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleGetGoodsBuyCartList 购物车-查询列表
func HandleGetGoodsBuyCartList(ctx *gin.Context) {
	param := &request.GetStewardGoodsBuyCartReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId, _ = helper.GetUserIdFromCtx(ctx)

	data := dao.NewGoodsDao().GetWxGoodsBuyCartList(param)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"host": global.Cfg.Ali.AliossPath,
		"data": data.Unwrap(),
	})(OK)
}

// HandleGetGoodsOrderDetails 查询订单详情
func HandleGetGoodsOrderDetails(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}

	param := &request.GetStewardGoodsOrderDetailsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId = userId

	data := dao.NewGoodsDao().GetWxGoodsOrdersDetailsReq(param)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"host": global.Cfg.Ali.AliossPath,
		"data": data.Unwrap(),
	})(OK)
}

// HandleGetGoodsOrderList 查询订单列表
func HandleGetGoodsOrderList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}

	param := &request.GetStewardGoodsOrderReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId = userId

	data := dao.NewGoodsDao().GetWxGoodsOrders(param)
	Response(ctx)("", CodeSuccess, ResponseConfigList{
		Host:       global.Cfg.Ali.AliossPath,
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// HandleGetGoodsTypeList 查询商品类型列表
func HandleGetGoodsTypeList(ctx *gin.Context) {
	param := &request.GetStewardGoodsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	data := dao.NewGoodsDao().GetWxGoodsTypeList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetGoodsList 查询商品列表
func HandleGetGoodsList(ctx *gin.Context) {
	param := &request.GetStewardGoodsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.BuyType = 1
	data := dao.NewGoodsDao().GetWxGoodsList(param)
	Response(ctx)("", CodeSuccess, ResponseConfigList{
		Host:       global.Cfg.Ali.AliossPath,
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

func HandleGetDrawGoodsPrizeList(ctx *gin.Context) {
	param := &request.GetStewardGoodsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewGoodsDao().GetDrawGoodsPrizeList()
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetDrawGoodsList 查询商品列表
func HandleGetDrawGoodsList(ctx *gin.Context) {
	param := &request.GetStewardGoodsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.BuyType = 2
	data := dao.NewGoodsDao().GetWxGoodsList(param)
	Response(ctx)("", CodeSuccess, ResponseConfigList{
		Host:       global.Cfg.Ali.AliossPath,
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// HandleCancelGoodsOrder 取消商品订单
func HandleCancelGoodsOrder(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}
	// 请求参数处理
	againParam := &request.AgainStewardOrderReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(againParam)).Unwrap()

	param := &request.CreateStewardOrderReq{}
	param.ClientIp = ctx.ClientIP()
	param.UserId = userId
	param.TradeNo = againParam.TradeNo

	orderData := dao.NewGoodsDao().GetGoodsOrdersListByTradeNo(againParam.TradeNo, helper.TypeNumGoodsOrderStatusWait)
	goodsOrderList := orderData.Unwrap().([]*goods.OrderUpdate)
	if len(goodsOrderList) == 0 {
		Response(ctx)("无效订单", CodeNeedLogin, nil)(Unauthorized)
		return
	}
	for _, item := range goodsOrderList {
		param.GoodsList = append(param.GoodsList, request.GoodsList{Id: item.GoodsId, Num: -item.BuyNum})
	}
	if err = dao.NewGoodsDao().UpdateGoodsNum(param); err != nil {
		Response(ctx)(err.Error(), CodeInternalServerError, nil)(OK)
		return
	}

	//err = dao.NewGoodsDao().UpdateGoodsOrders(&goods.OrderUpdate{TradeNo: againParam.TradeNo, Status: helper.TypeNumGoodsOrderStatusCancel})
	//if err != nil {
	//	Response(ctx)(err.Error(), CodeInternalServerError, nil)(OK)
	//	return
	//}

	Response(ctx)("success", CodeSuccess, true)(OK)
}

// HandleAgainGoodsOrder 重新支付 商品订单
func HandleAgainGoodsOrder(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}
	// 请求参数处理
	againParam := &request.AgainStewardOrderReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(againParam)).Unwrap()

	param := &request.CreateStewardOrderReq{}
	param.ClientIp = ctx.ClientIP()
	param.UserId = userId

	// 验证是否是黑名单 - 拉黑暂时只针对充电服务，充电服务的黑名单已移至小程序判断
	//if b := helper.CheckOpenidStatus(param.Openid); b {
	//	Response(ctx)("CodeInvalidOpenid", CodeInvalidOpenid, nil)(OK)
	//	return
	//}

	orderData := dao.NewGoodsDao().GetGoodsOrdersListByTradeNo(againParam.TradeNo, helper.TypeNumGoodsOrderStatusWait)
	goodsOrderList := orderData.Unwrap().([]*goods.OrderUpdate)
	if len(goodsOrderList) == 0 {
		Response(ctx)("无效订单", CodeNeedLogin, nil)(Unauthorized)
		return
	}
	if goodsOrderList[0].Status != helper.TypeNumGoodsOrderStatusWait {
		Response(ctx)("无效订单", CodeNeedLogin, nil)(Unauthorized)
		return
	}

	for _, item := range goodsOrderList {
		param.Amount += item.Amount
	}

	param.Key = "goods"
	resultData := ""
	orderLogic := logic.NewOrderLogic()
	orderLogic.TradeNo = goodsOrderList[0].TradeNo
	param.TradeNo = goodsOrderList[0].TradeNo
	if param.Amount > 0 {
		if resultData = orderLogic.HandleWxStewardGetAgainOrder(orderLogic.TradeNo); resultData == "" {
			if err = dao.NewGoodsDao().CancelGoodsOrder(param); err != nil {
				Response(ctx)(err.Error(), CodeInternalServerError, nil)(OK)
				return
			}
			Response(ctx)("订单已失效", CodeNeedLogin, nil)(Unauthorized)
			return
		}
	} else {
		_ = dao.NewGoodsDao().UpdateGoodsOrders(&goods.OrderUpdate{
			TradeNo: orderLogic.TradeNo,
			Status:  helper.TypeNumGoodsOrderStatusDonePay})
	}

	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"pay":     resultData,
		"order":   goodsOrderList,
		"donePay": param.Amount <= 0,
	})(OK)
}

// HandleCreateGoodsOrder 统一下单 商品订单
func HandleCreateGoodsOrder(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}
	// 请求参数处理
	param := &request.CreateStewardOrderReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.ClientIp = ctx.ClientIP()
	param.UserId = userId

	if param.Openid == "" || param.DeviceSn == "" {
		Response(ctx)("参数错误,请重新扫码", CodeError, nil)(OK)
		return
	}

	// 验证是否是黑名单  - 拉黑暂时只针对充电服务，充电服务的黑名单已移至小程序判断
	//if b := helper.CheckOpenidStatus(param.Openid); b {
	//	Response(ctx)("CodeInvalidOpenid", CodeInvalidOpenid, nil)(OK)
	//	return
	//}

	// 商品校验
	if goodsInvalidList, err := dao.NewGoodsDao().CheckGoodsNum(param); err != nil {
		Response(ctx)(err.Error(), CodeInternalServerError, false)(Error)
		return
	} else if len(goodsInvalidList) > 0 {
		Response(ctx)("部分商品失效", CodeError, goodsInvalidList)(OK)
		return
	}

	param.Key = "goods"
	resultData := &wxpay.PayData{}
	orderLogic := logic.NewOrderLogic()
	goodsLogic := logic.NewOrderLogicGoods(param, orderLogic.TradeNo)
	if goodsLogic.Repetition {
		Response(ctx)("下单过于频繁", CodeError, false)(Error)
		return
	}
	goodsLogic.InsertOrdersLogicGoods(param)
	if len(goodsLogic.AddGoodsOrderList) == 0 {
		Response(ctx)("下单失败", CodeInternalServerError, false)(Error)
		return
	}
	if err := dao.NewGoodsDao().UpdateGoodsNum(param); err != nil {
		Response(ctx)("下单失败-数量", CodeInternalServerError, false)(Error)
		return
	}

	if goodsLogic.Amount > 0 {
		param.Amount = goodsLogic.Amount
		respData, err := orderLogic.HandleWxStewardUnifiedOrder(param)
		if err != nil {
			Response(ctx)(err.Error(), CodeError, respData)(Error)
			return
		}
		resultData = orderLogic.HandleMakeSign(respData)
		_ = orderLogic.HandleWxStewardSetAgainOrder(orderLogic.TradeNo, resultData.ToJson())
	} else {
		_ = dao.NewGoodsDao().UpdateGoodsOrders(&goods.OrderUpdate{
			TradeNo: orderLogic.TradeNo,
			Status:  helper.TypeNumGoodsOrderStatusDonePay})
	}

	dao.NewGoodsDao().EmptyGoodsBuyCart(&request.SetGoodsBuyCartReq{PlaceId: param.PlaceId, MemberId: userId})
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"pay":     resultData.ToJson(),
		"order":   goodsLogic.AddGoodsOrderList,
		"donePay": goodsLogic.Amount <= 0,
	})(OK)
}

// HandleGetRoomTypeInfo 查询当前设备房间类型
func HandleGetRoomTypeInfo(ctx *gin.Context) {
	param := &request.GetRoomTypeListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewRoomDaoDao().GetWxRoomTypeInfo(param)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"host": global.Cfg.Ali.AliossPath,
		"data": data.Unwrap(),
	})(OK)
}

// HandleGetRoomTypeList 查询房间类型
func HandleGetRoomTypeList(ctx *gin.Context) {
	param := &request.GetRoomTypeListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	data := dao.NewRoomDaoDao().GetWxRoomTypeList(param)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"host": global.Cfg.Ali.AliossPath,
		"data": data.Unwrap(),
	})(OK)
}

// HandleGetRoomTypeList 查询房间订单详情
func HandleGetRoomOrderDetails(ctx *gin.Context) {
	userId, _ := helper.GetUserIdFromCtx(ctx)
	param := &request.GetRoomOrderDetailsReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.MemberId = userId

	data := dao.NewRoomDaoDao().GetWxRoomOrderDetails(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleCreateRoomOrder 统一下单 续住订单
func HandleCreateRoomOrder(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		Response(ctx)(err.Error(), CodeNeedLogin, nil)(Unauthorized)
		return
	}
	// 请求参数处理
	param := &request.CreateStewardOrderReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.ClientIp = ctx.ClientIP()
	param.UserId = userId

	if param.Openid == "" || param.DeviceSn == "" {
		Response(ctx)("参数错误,请重新扫码", CodeError, nil)(OK)
		return
	}

	// 验证是否是黑名单  - 拉黑暂时只针对充电服务，充电服务的黑名单已移至小程序判断
	//if b := helper.CheckOpenidStatus(param.Openid); b {
	//	Response(ctx)("CodeInvalidOpenid", CodeInvalidOpenid, nil)(OK)
	//	return
	//}

	roomTypeData := dao.NewRoomDaoDao().GetRoomTypeDetails(param.RoomTypeId).Unwrap().(*request.RoomTypeListRes)
	if roomTypeData.Id == 0 || roomTypeData.Status != 1 {
		Response(ctx)("参数错误，无空房", CodeError, nil)(OK)
		return
	}

	OrderStatus := 1
	param.Key = "room"
	param.Amount = roomTypeData.Price - roomTypeData.DiscountsAmount
	dayLen := utils.GetDayLen(param.InTime, param.LeaveTime)
	if param.Amount <= 0 {
		param.Amount = 0
		OrderStatus = 4
	} else {
		param.Amount = param.Amount * dayLen
	}

	// 下订单业务逻辑
	resultData := &wxpay.PayData{}
	orderLogic := logic.NewOrderLogic()
	if param.Amount > 0 {
		respData, err := orderLogic.HandleWxStewardUnifiedOrder(param)
		if err != nil {
			Response(ctx)(err.Error(), CodeError, respData)(Error)
			return
		}
		resultData = orderLogic.HandleMakeSign(respData)
	}

	// 创建本地订单
	orderData := dao.NewRoomDaoDao().CreateRoomOrder(&room.OrderDb{
		MemberId:        userId,
		DayLen:          dayLen,
		UserId:          roomTypeData.UserId,
		PlaceId:         roomTypeData.PlaceId,
		TypeId:          param.RoomTypeId,
		RoomId:          param.RoomId,
		DiscountsAmount: roomTypeData.DiscountsAmount,
		Price:           roomTypeData.Price,
		Amount:          param.Amount,
		Status:          uint8(OrderStatus),
		PayType:         1,
		ThirdTradeNo:    resultData.Get("out_trade_no"),
		TradeNo:         orderLogic.TradeNo,
		DeviceSn:        param.DeviceSn,
		UserName:        roomTypeData.UserName,
		PlaceName:       roomTypeData.PlaceName,
		InTime:          param.InTime,
		LeaveTime:       param.LeaveTime,
		MemberPhone:     param.MemberPhone,
		MemberName:      param.MemberName,
	})

	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"pay":   resultData.ToJson(),
		"order": orderData.Unwrap(),
	})(OK)
}

// HandleCreateBelowLineRoomOrder 统一下单 续住订单
func HandleCreateBelowLineRoomOrder(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	// 请求参数处理
	param := &request.CreateStewardOrderReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.ClientIp = ctx.ClientIP()
	param.UserId = userId

	if param.Openid == "" || param.DeviceSn == "" {
		Response(ctx)("参数错误,请重新扫码", CodeError, nil)(OK)
		return
	}

	roomTypeData := dao.NewRoomDaoDao().GetRoomTypeDetails(param.RoomTypeId).Unwrap().(*request.RoomTypeListRes)
	if roomTypeData.Id == 0 || roomTypeData.Status != 1 {
		Response(ctx)("参数错误，无空房", CodeError, nil)(OK)
		return
	}

	orderNum := dao.NewRoomDaoDao().GetPlaceRoomOrderCountBy24(param.UserId).Unwrap().(int64)
	if orderNum >= 10 {
		Response(ctx)("操作频繁，请稍后重试", CodeError, nil)(OK)
		return
	}

	OrderStatus := 4
	param.Key = "room"
	param.Amount = roomTypeData.Price - roomTypeData.DiscountsAmount
	dayLen := utils.GetDayLen(param.InTime, param.LeaveTime)
	if param.Amount <= 0 {
		param.Amount = 0
	} else {
		param.Amount = param.Amount * dayLen
	}

	// 下订单业务逻辑
	orderLogic := logic.NewOrderLogic()

	// 创建本地订单
	orderData := dao.NewRoomDaoDao().CreateRoomOrder(&room.OrderDb{
		MemberId:        userId,
		DayLen:          dayLen,
		UserId:          roomTypeData.UserId,
		PlaceId:         roomTypeData.PlaceId,
		TypeId:          param.RoomTypeId,
		RoomId:          param.RoomId,
		DiscountsAmount: roomTypeData.DiscountsAmount,
		Price:           roomTypeData.Price,
		Amount:          param.Amount,
		Status:          uint8(OrderStatus),
		PayType:         5,
		ThirdTradeNo:    "",
		TradeNo:         orderLogic.TradeNo,
		DeviceSn:        param.DeviceSn,
		UserName:        roomTypeData.UserName,
		PlaceName:       roomTypeData.PlaceName,
		InTime:          param.InTime,
		LeaveTime:       param.LeaveTime,
		MemberPhone:     param.MemberPhone,
		MemberName:      param.MemberName,
		Message:         `[线下支付]` + param.Message,
	})

	dataInfo := dao.NewRoomDaoDao().GetRoomOrderPlace(orderLogic.TradeNo)
	mParam := &placemodel.PlacesServiceNotification{
		UserId:           roomTypeData.UserId,
		MemberId:         dataInfo.MemberId,
		PlaceId:          dataInfo.PlaceId,
		PlaceName:        dataInfo.PlaceName,
		PlaceRoom:        dataInfo.Room,
		MemberPhone:      dataInfo.Phone,
		DeviceSn:         dataInfo.DeviceSn,
		NotificationType: helper.TypeNumNotificationTypeContinues,
		Status:           helper.TypeNumNotificationStatusWait,
		Info:             "房间续住线下支付",
		ServiceInfo:      []string{dataInfo.InTime, dataInfo.LeaveTime},
		Message:          `[线下支付]` + param.Message,
	}
	if notificationsInfo := logic.NewShWxNotificationsLogic().InitNotificationsData(mParam); notificationsInfo.Id <= 0 {
		return
	} else {
		updateParam := &room.OrderDb{
			Amount:         param.Amount,
			TradeNo:        orderLogic.TradeNo,
			NotificationId: notificationsInfo.Id,
			Status:         helper.TypeNumRoomOrderStatusDoneWait,
		}
		if data := dao.NewRoomDaoDao().UpdateRoomOrder(updateParam); data.Err != nil {
			logs.Error("UpdateRoomOrder:" + data.Err.Error())
			return
		}
	}

	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"pay":   nil,
		"order": orderData.Unwrap(),
	})(OK)
}
